{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "plato_colab.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "olul0mnbnLoL"
      },
      "source": [
        "# Running Plato in Google's Colab Notebooks\n",
        "\n",
        "## 1. Preparation\n",
        "\n",
        "### Activate GPU support\n",
        "\n",
        "To activate GPU support, click on `Runtime > Change runtime type` in the notebook menu and choose `GPU` as the hardware accelerator. To check whether the GPU is available for computation, we import the deep learning framework [PyTorch](https://pytorch.org/):"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2RB_ZJrZmugO"
      },
      "source": [
        "import torch\n",
        "torch.cuda.is_available()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SkkBTKHtuTVy"
      },
      "source": [
        "If successful, the output of the cell above should print `True`.\n",
        "\n",
        "### Use Google Drive\n",
        "\n",
        "Since Google Colab removes all the files that you have downloaded or created when you end a session, the best option is to use GitHub to store your code, and Google Drive to store your datasets, logs, and anything else that would normally reside on your filesystem but wouldn’t be tracked by a git repo.\n",
        "\n",
        "When you run the code below, you will need to click a link and follow a process that takes a few seconds. When the process is complete, all of your drive files will be available via ‘/content/drive’ on your Colab instance, and this will allow you to structure your projects in the same way you would if you were using a cloud server."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "z5gW8LQivFHs"
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/root/drive')\n",
        "root_path = '/root/drive/My\\ Drive'\n",
        "%cd $root_path"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "5xlN-jMfoEpp"
      },
      "source": [
        "## 2. Installing Plato with PyTorch\n",
        "\n",
        "Clone Plato's public git repository on GitHub to your Google drive. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P175hLMavyOg"
      },
      "source": [
        "!git clone https://github.com/TL-System/plato"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "barbbjpfZGy-"
      },
      "source": [
        "Then install the required Python packages:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tCUSaI9fXAjB"
      },
      "source": [
        "!pip install -U PyYAML\n",
        "!pip install -r $root_path/plato/requirements.txt"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cTfvXkYC1j45"
      },
      "source": [
        "Change the permissions on `plato` directory:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oW6q5XWi1v4i"
      },
      "source": [
        "!chmod -R ugo+rX $root_path/plato/\n",
        "%cd $root_path/plato/"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YKRnIwDPZd9C"
      },
      "source": [
        "## 3. Running Plato\n",
        "\n",
        "### Make sure you don’t get disconnected\n",
        "\n",
        "Run the following cell when you plan to do a long training to avoid getting disconnected in the middle of it."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GRemJcsAM65-"
      },
      "source": [
        "%%javascript\n",
        "function ClickConnect(){\n",
        "console.log(\"Working\");\n",
        "document.querySelector(\"colab-toolbar-button#connect\").click()\n",
        "}setInterval(ClickConnect,60000)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sDhjrvbZ6NC_"
      },
      "source": [
        "**Note:** Please use this responsibly. Getting booted from Colab is very annoying, but it is done to make resources available for others when you’re not actively using them. \n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fPsxX4AtgviS"
      },
      "source": [
        "### Setting up Weights and Biases\n",
        "\n",
        "Support for logging using Weights and Biases (https://wandb.com) is built-in. It will prompt you to enter your key when starting your first run. If you don't wish to use Weights and Biases, set it to `offline`:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vDYM4bSZhNAK"
      },
      "source": [
        "!wandb offline"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mlmSKKaSk6P3"
      },
      "source": [
        "### Running Plato using a configuration file\n",
        "\n",
        "To start a federated learning training workload, run `run` from Plato's home directory. For example:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ivm8dSigaV7Y"
      },
      "source": [
        "!./run -s 0.0.0.0:8000 -c ./configs/MNIST/fedavg_lenet5.yml"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Jv1jzv0Ynd17"
      },
      "source": [
        "Here, `fedavg_lenet5.yml` is a sample configuration file that uses Federated Averaging as the federated learning algorithm, and LeNet5 as the model. Other configuration files under `plato/configs/` could also be used here.\n",
        "\n",
        "\n"
      ]
    }
  ]
}